import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;

/**
 * 解析Cosmeticanalysis成分
 * 
 * @author abc
 *
 */
public class SpliderCosmeticanalysisComposition extends UDF {

	public String evaluate(String html) {
		try {
			// HttpClientAPI HttpClientAPI = new HttpClientAPI();
			// HttpResult doGet = HttpClientAPI
			// .doGet("http://www.cosmeticanalysis.com/cosmetic-ingredients/1-2-6-hexanetriol.html");
			// Document document = Jsoup.parse(doGet.getBody());
			// String html = document.getElementById("content").toString();
			JSONObject returnObj = new JSONObject();
			if (StringUtils.isNotBlank(html)) {
				Document doc = Jsoup.parse(html);

				// 获取成分列表
				Elements ingredientClass = doc.getElementsByClass("assetTypes clear");
				if (null != ingredientClass && ingredientClass.size() == 1) {
					Element ingredientId = ingredientClass.get(0);
					Elements dts = ingredientId.getElementsByTag("dt");
					Elements dds = ingredientId.getElementsByTag("dd");
					// key和value堆成
					if (null != dts && null != dds && dts.size() == dds.size()) {
						for (int i = 0; i < dts.size(); i++) {
							Element element = dts.get(i);
							if (StringUtils.equals("INCI name", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("INCI name", dds.get(i).text());
							}
							if (StringUtils.equals("Alternative names", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("Alternative names", dds.get(i).text());
							}
							if (StringUtils.equals("Origin", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("Origin", dds.get(i).text());
							}
							if (StringUtils.equals("Definition", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("Definition", dds.get(i).text());
							}
							if (StringUtils.equals("INCI function", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("INCI function", dds.get(i).text());
							}
							if (StringUtils.equals("Characteristics", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("Characteristics", dds.get(i).text());
							}
							if (StringUtils.equals("CAS-No.", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("CAS-No", dds.get(i).text());
							}
							if (StringUtils.equals("EINECS/EILINCS-No.", element.text())) {
								System.out.println(element.text());
								System.out.println(dds.get(i).text());
								returnObj.put("EINECS/EILINCS-No", dds.get(i).text());
							}
						}
					}

					return returnObj.toJSONString();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
